package com.jinke.uang.utils;

import android.text.TextUtils;
import android.view.TextureView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

public class MaskUtils {
    private static Map<String, String> map = new HashMap<>();
    public static String MASK_STR = "  ***  ";

    public static void setText(EditText editText, String key, String text) {
        if (TextUtils.isEmpty(text)) {
            return;
        } else if (text.length() < 5) {
            editText.setText(text);
            return;
        }
        String newString = text.substring(0, 3) + MASK_STR + text.substring(text.length() - 2, text.length());
        editText.setText(newString);
        map.put(key, text);
    }

    public static void setText(TextView textView, String text) {
        if (text.length() < 5) {
            textView.setText(text);
            return;
        }
        String newString = text.substring(0, 3) + MASK_STR + text.substring(text.length() - 2, text.length());
        textView.setText(newString);
    }


    public static void setText(EditText editText, String key, String text, int start, int end) {
        if (end - start < text.length()) {
            editText.setText(text);
            return;
        }
        String newString = text.substring(0, start) + MASK_STR + text.substring(end, text.length());
        editText.setText(newString);
        map.put(key, text);
    }

    public static void setText(TextView textView, String key, String text, int start, int end) {
        if (end - start < text.length()) {
            textView.setText(text);
            return;
        }
        String newString = text.substring(0, start) + MASK_STR + text.substring(end, text.length());
        textView.setText(newString);
        map.put(key, text);
    }

    public static String getOriginText(String key, String text) {
        if (text.contains(MASK_STR)) {
            String origin = map.get(key);
            if (TextUtils.isEmpty(origin)) return text;
            return origin;
        }

        return text;


    }


    public static void clearText(View view) {
        try {
            TextView textView = (TextView) view;
            if (textView.getText().toString().contains(MASK_STR))
                textView.setText("");
        } catch (Exception e) {

        }

    }

    /**
     * 掩码显示字符串
     *
     * @param str
     * @param startCount  开头显示位数
     * @param endCount    结尾显示位数
     * @param centerCount 中间*号组数 每组几位（默认为1组，每组4个*）
     * @return
     */
    public static String getMaskCodeStr(String str, int startCount, int endCount, int... centerCount) {
        if (str.length() <= startCount + endCount) return str;
        StringBuilder sb = new StringBuilder();
        sb.append(str.substring(0, startCount));
        sb.append(" ");

        int asterisk = 1;
        int asteriskCount = 4;
        if (centerCount.length == 1) {
            if (centerCount[0] > 1) {
                asterisk = centerCount[0];
            }
        } else if (centerCount.length == 2) {
            if (centerCount[0] > 1) {
                asterisk = centerCount[0];
            }
            if (centerCount[0] > 0) {
                asteriskCount = centerCount[1];
            }
        }
        StringBuilder asteriskSb = new StringBuilder();
        for (int i = 0; i < asteriskCount; i++) {
            asteriskSb.append("*");
        }
        for (int i = 0; i < asterisk; i++) {
            sb.append(asteriskSb.toString());
            sb.append(" ");
        }
        sb.append(str.substring(str.length() - endCount));
        return sb.toString();
    }
}
